<html>
<head><meta charset="utf-8"><title>Is this a dangling Cell ? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html">Is this a dangling Cell ?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209329069"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209329069" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209329069">(Sep 07 2020 at 21:40)</a>:</h4>
<p>I'm seeing this code in a test:</p>
<div class="codehilite"><pre><span></span><code><span class="cm">/* ... */</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">b</span>: <span class="kp">&amp;</span><span class="nc">Cell</span><span class="o">&lt;</span><span class="n">Point</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Cell</span>::<span class="n">new</span><span class="p">(</span><span class="n">a</span><span class="p">);</span><span class="w"></span>
<span class="cm">/* ... */</span><span class="w"></span>
</code></pre></div>


<p>I think <code>b</code> is dangling here, am I correct ?</p>



<a name="209329090"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209329090" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209329090">(Sep 07 2020 at 21:41)</a>:</h4>
<p>No, that would not compile</p>



<a name="209329108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209329108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209329108">(Sep 07 2020 at 21:41)</a>:</h4>
<p>Isn't it like <code>CStr::new("some text").as_ptr()</code> ?</p>



<a name="209329155"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209329155" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209329155">(Sep 07 2020 at 21:42)</a>:</h4>
<p>as_ptr() removes the lifetime, as it returns *const c_char. This has no such cast.</p>



<a name="209329161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209329161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209329161">(Sep 07 2020 at 21:42)</a>:</h4>
<p>oh yeah, I forgot the implicit lifetime</p>



<a name="209329167"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209329167" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209329167">(Sep 07 2020 at 21:42)</a>:</h4>
<p>I shouldn't code when tired</p>



<a name="209329168"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209329168" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209329168">(Sep 07 2020 at 21:42)</a>:</h4>
<p>Note, however, that the <em>general</em> pattern might compile -- just this particular case probably does not, as Cell has UnsafeCell in it and as such should not be implicitly promoted I suspect.</p>



<a name="209329183"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209329183" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209329183">(Sep 07 2020 at 21:43)</a>:</h4>
<p>The example I gave compiles just fine</p>



<a name="209329224"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209329224" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209329224">(Sep 07 2020 at 21:44)</a>:</h4>
<p>that should work fine, yeah</p>



<a name="209329254"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209329254" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209329254">(Sep 07 2020 at 21:45)</a>:</h4>
<p>hm I would not have expected that to work, given that this does not:</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">bar</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nb">&#39;static</span><span class="w"> </span><span class="n">Cell</span><span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">&amp;</span><span class="n">Cell</span>::<span class="n">new</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="209329268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209329268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209329268">(Sep 07 2020 at 21:45)</a>:</h4>
<p>It doesn't create a static, the temporary is just made to live as long as the binding</p>



<a name="209329311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209329311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209329311">(Sep 07 2020 at 21:46)</a>:</h4>
<p>ah, right</p>



<a name="209329318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209329318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209329318">(Sep 07 2020 at 21:46)</a>:</h4>
<p>I forgot the binding influenced that (vs. just statement)</p>



<a name="209329328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209329328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209329328">(Sep 07 2020 at 21:46)</a>:</h4>
<p>Well, I learned something new <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span> thanks !</p>



<a name="209356238"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209356238" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209356238">(Sep 08 2020 at 08:03)</a>:</h4>
<p>Afaik</p>
<div class="codehilite"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">expr</span><span class="p">();</span><span class="w"></span>
</code></pre></div>


<p>gets lowered to</p>
<div class="codehilite"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">_x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">expr</span><span class="p">();</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">_x</span><span class="p">;</span><span class="w"></span>
</code></pre></div>


<p>so this compiles as long as x is not moved out of the function. To do that <code>expr()</code> must get promoted to a constant, which does not work with method calls (I think <code>size_of</code> is an exception here)</p>



<a name="209356298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209356298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209356298">(Sep 08 2020 at 08:03)</a>:</h4>
<p><a href="https://github.com/rust-lang/const-eval/blob/master/promotion.md">https://github.com/rust-lang/const-eval/blob/master/promotion.md</a></p>



<a name="209357328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209357328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209357328">(Sep 08 2020 at 08:17)</a>:</h4>
<p>I remember reading somewhere that temporaries created in an expression are destroyed "at the next semicolon". What determines which strategy it uses?</p>



<a name="209357905"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209357905" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209357905">(Sep 08 2020 at 08:24)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="k">struct</span> <span class="nc">Foo</span><span class="p">;</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">do_something</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;doing something&quot;</span><span class="p">);</span><span class="w"> </span><span class="bp">self</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="nb">Drop</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">drop</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;dropping foo&quot;</span><span class="p">);</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Foo</span><span class="p">.</span><span class="n">do_something</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;done&quot;</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="c1">// Prints:</span>
<span class="w">    </span><span class="c1">// doing something</span>
<span class="w">    </span><span class="c1">// dropping foo</span>
<span class="w">    </span><span class="c1">// done</span>

<span class="w">    </span><span class="c1">// std::mem::drop(x); // borrowck error</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="209358152"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209358152" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209358152">(Sep 08 2020 at 08:27)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="k">struct</span> <span class="nc">Foo</span><span class="p">;</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">new</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">do_something</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;doing something&quot;</span><span class="p">);</span><span class="w"> </span><span class="bp">self</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="nb">Drop</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">drop</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;dropping foo&quot;</span><span class="p">);</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// let x = Foo::do_something(&amp;Foo::new()); // error</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Foo</span>::<span class="n">new</span><span class="p">();</span><span class="w"> </span><span class="c1">// OK</span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;done&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">std</span>::<span class="n">mem</span>::<span class="nb">drop</span><span class="p">(</span><span class="n">x</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Prints:</span>
<span class="w">    </span><span class="c1">// done</span>
<span class="w">    </span><span class="c1">// dropping foo</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="209358404"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209358404" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209358404">(Sep 08 2020 at 08:30)</a>:</h4>
<p>Apparently storing a reference to a temporary directly to a local variable triggers promotion (not sure that's the right word, but the temp now acts as though it was declared with <code>let temp = ...; let x = &amp;temp;</code>), but passing it through a function  (even an identity function) causes it to be classified as local to the expression and promotion doesn't happen</p>



<a name="209359007"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359007" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359007">(Sep 08 2020 at 08:36)</a>:</h4>
<p>I am not sure, maybe assigning <code>&amp;expr</code> to a place adds an implicit temporary</p>



<a name="209359187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359187">(Sep 08 2020 at 08:38)</a>:</h4>
<p>but promotion is not the right term here, as promotion only occurs is an expression is "promoted" to a constant, which is not hgappening with <code>Foo::new()</code> here. Maybe <span class="user-mention" data-user-id="120791">@RalfJ</span> can shine some more light on this <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="209359437"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359437" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359437">(Sep 08 2020 at 08:41)</a>:</h4>
<p>No this is not promotion, this is what I call the "enclosing scope" rule: when there is an <code>&amp;expr</code> as the outermost operator in a <code>let</code>, then the temporary for <code>expr</code> gets put into the enclosing scope of this <code>let</code>, and only gets destroyed when that scope is left.</p>



<a name="209359461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359461">(Sep 08 2020 at 08:41)</a>:</h4>
<p>This is completely orthogonal to constant promotion, which gives things the <code>'static</code> lifetime.</p>



<a name="209359465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359465">(Sep 08 2020 at 08:41)</a>:</h4>
<p>yeah, I didn't demonstrate it but <code>let x: &amp;'static Foo = &amp;Foo::new();</code> doesn't work, so it's not promotion in the sense of constant promotion. Interestingly <code>let x: &amp;'static Foo = &amp;Foo;</code> also doesn't work so maybe I don't understand constant promotion rules</p>



<a name="209359482"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359482" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359482">(Sep 08 2020 at 08:41)</a>:</h4>
<p>maybe it's because <code>Foo</code> has a destructor</p>



<a name="209359586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359586">(Sep 08 2020 at 08:42)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F/near/209357328">said</a>:</p>
<blockquote>
<p>I remember reading somewhere that temporaries created in an expression are destroyed "at the next semicolon". What determines which strategy it uses?</p>
</blockquote>
<p>it's "at the next semicolon, except if the enclosing scope rule applies, in which case it's at the end of that enclosing scope".</p>



<a name="209359597"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359597" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359597">(Sep 08 2020 at 08:42)</a>:</h4>
<p>(except if promotion applies, in which case it's never...)</p>



<a name="209359610"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359610" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359610">(Sep 08 2020 at 08:43)</a>:</h4>
<p>and enclosing scope rule only applies if it has the literal form <code>let x = &amp;...</code>?</p>



<a name="209359630"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359630" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359630">(Sep 08 2020 at 08:43)</a>:</h4>
<p>the enclosing scope is a phenomenon general to <code>let</code> bindings, isn't it?</p>



<a name="209359633"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359633" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359633">(Sep 08 2020 at 08:43)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F/near/209359610">said</a>:</p>
<blockquote>
<p>and enclosing scope rule only applies if it has the literal form <code>let x = &amp;...</code>?</p>
</blockquote>
<p>I think so but... not sure</p>



<a name="209359663"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359663" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359663">(Sep 08 2020 at 08:44)</a>:</h4>
<p>I learned about this from <span class="user-mention" data-user-id="119009">@eddyb</span>  and never dug deep enough to learn all the details</p>



<a name="209359709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359709">(Sep 08 2020 at 08:44)</a>:</h4>
<p>is this not the thing where <code>let</code> implicitly creates a scope?</p>



<a name="209359719"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359719" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359719">(Sep 08 2020 at 08:44)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281757">Jubilee</span> <a href="#narrow/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F/near/209359709">said</a>:</p>
<blockquote>
<p>is this not the thing where <code>let</code> implicitly creates a scope?</p>
</blockquote>
<p>maybe^^</p>



<a name="209359755"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359755" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359755">(Sep 08 2020 at 08:44)</a>:</h4>
<p><a href="https://doc.rust-lang.org/nomicon/lifetimes.html?highlight=desugar#lifetimes">https://doc.rust-lang.org/nomicon/lifetimes.html?highlight=desugar#lifetimes</a> this, ne?</p>



<a name="209359775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359775">(Sep 08 2020 at 08:45)</a>:</h4>
<p><span class="user-mention silent" data-user-id="307289">Poliorcetics</span> <a href="#narrow/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F/near/209329108">said</a>:</p>
<blockquote>
<p>Isn't it like <code>CStr::new("some text").as_ptr()</code> ?</p>
</blockquote>
<p>this desugars to <code>&amp;(CStr::new("some text")).as_ptr()</code>; notice how the <code>&amp;</code> is not on the outside, so the enclosing scope rule dos not apply</p>



<a name="209359812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359812">(Sep 08 2020 at 08:45)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281757">Jubilee</span> <a href="#narrow/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F/near/209359755">said</a>:</p>
<blockquote>
<p><a href="https://doc.rust-lang.org/nomicon/lifetimes.html?highlight=desugar#lifetimes">https://doc.rust-lang.org/nomicon/lifetimes.html?highlight=desugar#lifetimes</a> this, ne?</p>
</blockquote>
<p>that looks suspiciously pre-NLL to me...</p>



<a name="209359817"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359817" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359817">(Sep 08 2020 at 08:45)</a>:</h4>
<p>I believe it is the way lifetimes desugar like so:</p>
<div class="codehilite"><pre><span></span><code><span class="na">&#39;a</span>: <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span>: <span class="kt">i32</span> <span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="na">&#39;b</span>: <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">z</span>: <span class="kp">&amp;</span><span class="na">&#39;b</span><span class="w"> </span><span class="kt">i32</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="na">&#39;c</span>: <span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span>: <span class="kp">&amp;</span><span class="na">&#39;b</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="na">&#39;b</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"></span>
<span class="w">            </span><span class="n">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">y</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="209359824"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359824" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359824">(Sep 08 2020 at 08:46)</a>:</h4>
<p>well yes, probably.</p>



<a name="209359881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359881">(Sep 08 2020 at 08:46)</a>:</h4>
<p>I don't think it's been _completely_ invalidated in principle, though, just complicated?</p>



<a name="209359930"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359930" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359930">(Sep 08 2020 at 08:47)</a>:</h4>
<p>the fact that <code>y</code> and <code>z</code> get reordered is also odd</p>



<a name="209359955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209359955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209359955">(Sep 08 2020 at 08:47)</a>:</h4>
<p>that doesn't seem right</p>



<a name="209360048"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209360048" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209360048">(Sep 08 2020 at 08:48)</a>:</h4>
<p>anyway, I got to go... I don't think I can answer all the questions here, but I hope I was able to point in the right direction^^ One issue is that AFAIK the "enclosing scope rule" doesn't actually have a name (beyond me calling it like that) so it's rather hard to search for.</p>



<a name="209360085"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209360085" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209360085">(Sep 08 2020 at 08:49)</a>:</h4>
<p>another keyword to look for might be "temporary"; this is all about how rustc handles temporaries created by <code>&amp;expr</code> where <code>expr</code> is not a place expression (but a value expression)</p>



<a name="209360210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209360210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209360210">(Sep 08 2020 at 08:50)</a>:</h4>
<p>In terms of scopes like that, the enclosing scope rule is what turns this:</p>
<div class="codehilite"><pre><span></span><code><span class="na">&#39;x</span>: <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span>: <span class="kp">&amp;</span><span class="nc">Foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="na">&#39;temp</span>: <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Foo</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="n">temp</span><span class="w"></span>
<span class="w">        </span><span class="c1">// temp destroyed here</span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="c1">// x destroyed here</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>(which would not compile) into this:</p>
<div class="codehilite"><pre><span></span><code><span class="na">&#39;temp</span>: <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Foo</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="na">&#39;x</span>: <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span>: <span class="kp">&amp;</span><span class="nc">Foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">temp</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="c1">// x destroyed here</span>

<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="c1">// temp destroyed here</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>Note that the scopes are oppositely nested, and the destruction order is reversed</p>



<a name="209360750"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209360750" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209360750">(Sep 08 2020 at 08:56)</a>:</h4>
<p>My understanding is that NLL doesn't make the basic understanding of implicit scopes encircling each other completely invalid, it just lets rustc do magic tricks. <a href="https://www.youtube.com/watch?v=xb6rqAxkTK0">https://www.youtube.com/watch?v=xb6rqAxkTK0</a></p>
<div class="youtube-video message_inline_image"><a data-id="xb6rqAxkTK0" href="https://www.youtube.com/watch?v=xb6rqAxkTK0"><img src="https://i.ytimg.com/vi/xb6rqAxkTK0/default.jpg"></a></div>



<a name="209386894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209386894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209386894">(Sep 08 2020 at 13:38)</a>:</h4>
<p><a href="https://doc.rust-lang.org/reference/destructors.html#temporary-lifetime-extension">https://doc.rust-lang.org/reference/destructors.html#temporary-lifetime-extension</a></p>



<a name="209761636"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Is%20this%20a%20dangling%20Cell%20%3F/near/209761636" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F.html#209761636">(Sep 11 2020 at 09:59)</a>:</h4>
<p><span class="user-mention silent" data-user-id="138448">cuviper</span> <a href="#narrow/stream/122651-general/topic/Is.20this.20a.20dangling.20Cell.20.3F/near/209386894">said</a>:</p>
<blockquote>
<p><a href="https://doc.rust-lang.org/reference/destructors.html#temporary-lifetime-extension">https://doc.rust-lang.org/reference/destructors.html#temporary-lifetime-extension</a></p>
</blockquote>
<p>oh, it's in the destructor section. that is not at all where I would have expected it.<br>
this is observable not just through destructors but also through whether unsafe code working with pointers to temporaries is UB or not.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>